package cuit.cx.groupcomment.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cuit.cx.groupcomment.dao.GrouponDAO;
import cuit.cx.groupcomment.dao.OrdersDAO;
import cuit.cx.groupcomment.dao.PictureDAO;
import cuit.cx.groupcomment.dto.GroupDetail;
import cuit.cx.groupcomment.dto.GroupDto;
import cuit.cx.groupcomment.model.Groupon;
import cuit.cx.groupcomment.model.Orders;
import cuit.cx.groupcomment.model.Picture;
import cuit.cx.groupcomment.util.Constant;

public class GrouponService
{
	private GrouponDAO grouponDAO;
	private PictureDAO pictureDAO;
	private OrdersDAO ordersDAO;

	public GrouponDAO getGrouponDAO()
	{
		return grouponDAO;
	}

	public void setGrouponDAO(GrouponDAO grouponDAO)
	{
		this.grouponDAO = grouponDAO;
	}
	
	public PictureDAO getPictureDAO()
	{
		return pictureDAO;
	}

	public void setPictureDAO(PictureDAO pictureDAO)
	{
		this.pictureDAO = pictureDAO;
	}

	public OrdersDAO getOrdersDAO()
	{
		return ordersDAO;
	}

	public void setOrdersDAO(OrdersDAO ordersDAO)
	{
		this.ordersDAO = ordersDAO;
	}

	public List<GroupDto> findCurrentGroup(String time)
	{
		List<GroupDto> result = null;
		Long currTime = Long.parseLong(time);
		Map<String, Long> map = new HashMap<String, Long>();
		map.put("startTime", currTime);
		map.put("endTime", currTime);
		List<Groupon> list = grouponDAO.findCurrentGroup(map);
		if (list != null && !list.isEmpty())
		{
			result = new ArrayList<GroupDto>();
			for (Groupon groupon : list)
			{
				int grouponId = groupon.getId();
				String mainPicUrl = pictureDAO.getMainPicUrl(grouponId);
				GroupDto dto = new GroupDto();
				changeToDto(groupon, dto);
				dto.setMainPictureUrl(mainPicUrl);
				result.add(dto);
			}
		}
		return result;
	}
	
	public List<GroupDto> findPastGroup(String time)
	{
		List<GroupDto> result = null;
		Long currTime = Long.parseLong(time);
		Map<String, Long> map = new HashMap<String, Long>();
		map.put("endTime", currTime);
		List<Groupon> list = grouponDAO.findPastGroup(map);
		if (list != null && !list.isEmpty())
		{
			result = new ArrayList<GroupDto>();
			for (Groupon groupon : list)
			{
				int grouponId = groupon.getId();
				String mainPicUrl = pictureDAO.getMainPicUrl(grouponId);
				GroupDto dto = new GroupDto();
				changeToDto(groupon, dto);
				dto.setMainPictureUrl(mainPicUrl);
				result.add(dto);
			}
		}
		return result;
	}
	
	public GroupDetail findGrouponById(String id)
	{
		int groupId = Integer.parseInt(id);
		GroupDetail detail = new GroupDetail();;
		Groupon groupon = grouponDAO.findGrouponById(groupId);
		if (groupon != null)
		{
			
			/*dto = new GroupDto();
			String mainPicUrl = pictureDAO.getMainPicUrl(groupId);
			changeToDto(groupon, dto);
			dto.setMainPictureUrl(mainPicUrl);*/
			detail.setGroupon(groupon);
			List<Picture> pictures = pictureDAO.findPicByGroupId(groupId);
			detail.setPictures(pictures);
			List<Orders> orders = ordersDAO.findOrderByGroupId(groupId);
			detail.setOrderInfos(orders);
			detail.setResult(Constant.ACK_OK);
		}
		else
		{
			detail.setResult(Constant.ACK_FAIL);
		}
		return detail;
	}
	
	/**
	 * 	private Integer id;
	private Integer businissId;
	private String shopName;
	private String name;
	private String introdution;
	private String prompt;
	private Float nowPrice;
	private Float oldPrice;
	private Long startTime;
	private Long endTime;
	private String shopIntro;
	private String groupType;
	private String mainPictureUrl;
	 * @param groupon
	 * @param dto
	 */
	private void  changeToDto(Groupon groupon, GroupDto dto)
	{
		dto.setId(groupon.getId());
		dto.setBusinissId(groupon.getBusinissId());
		dto.setShopName(groupon.getShopName());
		dto.setName(groupon.getName());
		dto.setIntrodution(groupon.getIntrodution());
		dto.setPrompt(groupon.getPrompt());
		dto.setNowPrice(groupon.getNowPrice());
		dto.setOldPrice(groupon.getOldPrice());
		dto.setStartTime(groupon.getStartTime());
		dto.setEndTime(groupon.getEndTime());
		dto.setShopIntro(groupon.getShopIntro());
		dto.setGroupType(groupon.getGroupType());	
	}
}
